package com.example.zn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zn.dto.Result;
import com.example.zn.entity.Car;
import com.example.zn.service.CarService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@CrossOrigin
@Tag(name="订单详情管理",description = "商品和订单详情管理功能，添加订单")
public class CarController {
    @Autowired
    private CarService carService;


    @Operation(summary = "添加商品到订单",description = "根据提供的商品id和订单id执行订单详情添加操作")

    @PostMapping("/Car/add")
    public Result addDetails(Integer Dingdanid,Integer productid){
        //select * from car where orderid=? and productid=?
        QueryWrapper<Car> query = Wrappers.query(Car.class);
        query.eq("orderid",Dingdanid).eq("productid",productid);
        //执行条件查询

        Car one = carService.getOne(query);
        if(Objects.nonNull(one)){
            return Result.fail().setMsg("该歌曲已在歌单中存在");
        }

        Car car = new Car();
        car.setOrderid(Dingdanid);
        car.setProductid(productid);

        //执行添加
        if(carService.save(car)){
            return Result.ok().setMsg("成功加入歌单");
        }
        return Result.fail().setMsg("添加失败");
    }



    }

